package com.ch.sys.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ch.sys.common.Constants;
import com.ch.sys.filter.SessionUtils;
import com.ch.sys.model.Employee;
import com.ch.sys.model.Role;
import com.ch.sys.service.RoleService;
import com.ch.sys.service.EmployeeService;
import com.ch.sys.utils.Response;

@Controller
@RequestMapping(Constants.REST_WEB_URL)
public class EmployeeController extends BaseController {
	
	@Autowired
	EmployeeService employeeService;
	
	@Autowired
	RoleService roleService;
	
	/**
	 * 閸涙ê浼愰惂濠氭
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/employee/login",method=RequestMethod.POST)
	public Response login(@RequestBody Employee model, HttpServletRequest request) {
		Response response = Response.newResponse();
		
		String account =  model.getString(Employee.ACCOUNT);
		String password = model.getString(Employee.PASSWORD);
		
		Employee employee = employeeService.getByAccountPwd(account, password);
		if(employee == null) {
			return response.ACCOUNT_PASS_ERROR();
		}
		
		Role role = roleService.getByEmployeeId(employee.getLong(Employee.ID));
		employee.set("role", role);
		SessionUtils.set(request, employee);
		
		return response.ok(employee);
	}
}
